package com.gskj.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gskj.commonutils.ResultJson;
import com.gskj.eduservice.entity.EduCourse;
import com.gskj.eduservice.entity.EduTeacher;
import com.gskj.eduservice.service.IEduCourseDescriptionService;
import com.gskj.eduservice.service.IEduCourseService;
import com.gskj.eduservice.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :CodingFarmer_码农  Connor(康纳)_郭
 * @date : 17:59 20.5.20
 * @Company : http://www.gskj.com
 * @Version : 1.0
 *
 *
 *  前台首页面 调用 接口。
 *      查询 课程
 *      查询 讲师
 */
@RestController
@RequestMapping("/eduservice/indexfront")
@CrossOrigin
public class IndexFrontController {


    @Autowired
    private IEduCourseService iEduCourseService;
    @Autowired
    private IEduTeacherService iEduTeacherService;

    /***
     *  前台首页面 查询 课程、讲师
     * @return
     */
    @GetMapping("/index")
    public ResultJson getIndexInfo(){

        // 查询前8个热门课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        // 根据 浏览次数 降序查询
        courseQueryWrapper.orderByDesc("view_count");
        // 使用 last() 方法 拼接sql语句
        courseQueryWrapper.last("limit 8");

        List<EduCourse> eduCourseList = iEduCourseService.list(courseQueryWrapper);

        // 查询前4个讲师
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("sort");
        teacherQueryWrapper.last("limit 4");
        List<EduTeacher> teacherList = iEduTeacherService.list(teacherQueryWrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("courseList", eduCourseList);
        map.put("teacherList", teacherList);

        return ResultJson.success().data(map);
    }
}
